package com.dm.learn.java;

import java.util.Random;

public class StringUtils {
	public static final String numbers = "0123456789";
	public static final String lowLetters = "abcdefghijklmnopqrstuvwxyz";
	public static final String upLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	public static final int RANDOMTYPE_NUMBER = 1;
	public static final int RANDOMTYPE_LOWLETTERS = 2;
	public static final int RANDOMTYPE_UPLETTERS = 5;

	/**
	 * 获得指定长度的字符串
	 * 
	 * @param
	 * @return String
	 * @throws
	 */
	public static String getRandomStr(int type, int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(getRandomChar(type));
		}
		return sb.toString();
	}

	/**
	 * 随机获得一个字符
	 * 
	 * @param type
	 *            类型可以+
	 * @return char
	 * @throws
	 */
	public static char getRandomChar(int type) {
		Random random = new Random();
		switch (type) {
		case RANDOMTYPE_NUMBER:// 数字
			return numbers.charAt(random.nextInt(numbers.length()));
		case RANDOMTYPE_LOWLETTERS:// 小写
			return lowLetters.charAt(random.nextInt(lowLetters.length()));
		case RANDOMTYPE_UPLETTERS:// 大写
			return upLetters.charAt(random.nextInt(upLetters.length()));
		case RANDOMTYPE_NUMBER + RANDOMTYPE_LOWLETTERS:// 数字+小写
			return (numbers + lowLetters).charAt(random
					.nextInt((numbers + lowLetters).length()));
		case RANDOMTYPE_NUMBER + RANDOMTYPE_UPLETTERS:// 数字+大写
			return (numbers + upLetters).charAt(random
					.nextInt((numbers + upLetters).length()));
		case RANDOMTYPE_LOWLETTERS + RANDOMTYPE_UPLETTERS:// 大小写
			return (lowLetters + upLetters).charAt(random
					.nextInt((lowLetters + upLetters).length()));
		case RANDOMTYPE_NUMBER + RANDOMTYPE_LOWLETTERS + RANDOMTYPE_UPLETTERS:// 数字+小写+大写
			return (numbers + lowLetters + upLetters).charAt(random
					.nextInt((numbers + lowLetters + upLetters).length()));
		default:
			return (numbers + lowLetters + upLetters).charAt(random
					.nextInt((numbers + lowLetters + upLetters).length()));
		}
	}

	public static void main(String args[]) {
		System.out.println(getRandomStr(3, 5));
	}
}
